<?php

namespace app\user\controller;

use addons\wechat\model\WechatCaptcha;
use app\common\controller\Frontend;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Attachment;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 会员中心
 */
class Login extends Frontend
{
    protected $layout = 'default';
    protected $noNeedLogin = ['index', 'register', 'third','zhukao','findpassword'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        //监听注册登录退出的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        $this->model = new \app\admin\model\yexam\Subject;
    }
    /**
     * 注册会员
     */
    public function zhukao()
    {
        $url = $this->request->request('url', '', 'trim');
        $source = $this->request->request('source', '', 'trim');
        $ename = $this->request->request('ename', '', 'trim');
        $cn_name = '';
        if($ename){
            $row = $this->model->get(['en_name'=>$ename]);
            if($row) $cn_name = $row['short_name'];
        }
        $client = cmf_is_mobile() ? 'm' : 'pc';
        if ($this->auth->id) {
            $gourl = cmf_is_mobile() ? 'http://h5.zhukao.com.cn/index.php' : 'http://h5.zhukao.com.cn/index1.php';

            $agentId = '1146';
            $ageniUId = $this->auth->id;
            $safeKey = 'C864J3ZUZBZK1146';
            $aqkey = md5($agentId.$ageniUId.$safeKey);
            if(!empty($ename)){
                $keMu = $ename;
                $zhukaourl = $gourl.'?g=agentUrl&agentId='.$agentId.'&agentUId='.$ageniUId.'&keMu='.$keMu.'&f=Browser&aqKey='.$aqkey;
                //header('location:' .$zhukaourl);
                //exit;
            }else{
                $zhukaourl = $gourl.'?g=agentUrl&agentId='.$agentId.'&agentUId='.$ageniUId.'&f=Browser&aqKey='.$aqkey;
                //header('location:'. $zhukaourl);
                //exit;
            }
            //header('location:'.url('index/common/zkhead',array('zkurl'=>urlencode(urlencode($zhukaourl)))));exit;
            header('location:'.$zhukaourl);exit;
        }
        if ($this->request->isPost()) {
            // $username = $this->request->post('username');
            $password = $this->request->post('password');
            // $email = $this->request->post('email');
            $email = '';
            $mobile = $this->request->post('mobile', '');
            $captcha = $this->request->post('captcha');
            $token = $this->request->post('__token__');
            $rule = [
                // 'username'  => 'require|length:3,30',
                'password'  => 'require|length:6,30',
                // 'email'     => 'require|email',
                'mobile'    => 'regex:/^1\d{10}$/',
                '__token__' => 'require|token',
            ];

            $msg = [
                // 'username.require' => 'Username can not be empty',
                // 'username.length'  => 'Username must be 3 to 30 characters',
                'password.require' => '密码不能为空',
                'password.length'  => '密码长度须为6-30个字符',
                // 'email'            => 'Email is incorrect',
                'mobile'           => '手机号输入有误！',
				'password'           => 'Password is incorrect',
            ];
            $data = [
                // 'username'  => $username,
                'password'  => $password,
                // 'email'     => $email,
                'mobile'    => $mobile,
                '__token__' => $token,
            ];
            //验证码
            $captchaResult = true;
            $captchaType = config("fastadmin.user_register_captcha");
            if ($captchaType) {
                if ($captchaType == 'mobile') {
                    $captchaResult = Sms::check($mobile, $captcha, 'register');
//                } elseif ($captchaType == 'email') {
//                    $captchaResult = Ems::check($email, $captcha, 'register');
                } elseif ($captchaType == 'wechat') {
                    $captchaResult = WechatCaptcha::check($captcha, 'register');
                } elseif ($captchaType == 'text') {
                    $captchaResult = \think\Validate::is($captcha, 'captcha');
                }
            }
            if (!$captchaResult) {
                $this->error(__('Captcha is incorrect'));
            }
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            }
            $username = $mobile;
            if($this->auth->isUser($mobile)){
                if ($this->auth->login($mobile, $password)) {

                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }else{
                // Set default username, email...

                $extend['entry_platform'] = $client;
                $extend['url_source'] = $source;
                $extend['class_name'] = $ename;
                $extend['class_title'] = $cn_name;
                if ($this->auth->register($username, $password, $email, $mobile,$extend)) {

                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }

            //用户分析
            cmf_userId_set_cookieId($this->auth->id);
            cmf_cookieId_set_userId($this->auth->id);

            // 考试宝典处理
            $website = cmf_get_website_value();
            $ksbao_data = [
                'userName' => $username,
                'password' => $password,
                'client'   => $client,
                'partnercode' => $website['partnercode'],
            ];
            $ksbao_reg_url = cmf_get_ksbao_value('nregist');
            $json = cmf_send_post($ksbao_reg_url, $ksbao_data, $website['client_ip']);
            $return_arr = json_decode($json, true);
            if ($return_arr['status'] = 200) \session('guid', $return_arr['data']['guid']);

            $gourl = cmf_is_mobile() ? 'http://h5.zhukao.com.cn/index.php' : 'http://h5.zhukao.com.cn/index1.php';
            $agentId = '1146';
            $ageniUId = $this->auth->id;
            $safeKey = 'C864J3ZUZBZK1146';
            $aqkey = md5($agentId.$ageniUId.$safeKey);
            if(!empty($ename)){
                $keMu = $ename;
                $zhukaourl = $gourl.'?g=agentUrl&agentId='.$agentId.'&agentUId='.$ageniUId.'&keMu='.$keMu.'&f=Browser&aqKey='.$aqkey;
                //header('location:' .$zhukaourl);
                //exit;
            }else{
                $zhukaourl = $gourl.'?g=agentUrl&agentId='.$agentId.'&agentUId='.$ageniUId.'&f=Browser&aqKey='.$aqkey;
            }
            //echo("<script language='javascript'>window.top.location.href='".$zhukaourl."'</script>");exit;
            $this->success('登录成功',$zhukaourl);
        }
        //判断来源
        $referer = $this->request->server('HTTP_REFERER');
        if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
            && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
            $url = $referer;
        }
        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        $this->view->assign('url', $url);
        $this->view->assign('ename', $ename);
        $this->view->assign('cn_name', $cn_name);
        $this->view->assign('source', $source);
        $this->view->assign('title', __('登陆'));
        return $this->view->fetch();
    }

    /**
     * 注册会员
     */
    public function index()
    {
        $url = $this->request->request('url', '', 'trim');
        $source = $this->request->request('source', '', 'trim');
        $ename = $this->request->request('ename', '', 'trim');
        $cn_name = '';
        if($ename){
            $row = $this->model->get(['en_name'=>$ename]);
            if($row) $cn_name = $row['short_name'];
        }
        $client = cmf_is_mobile() ? 'm' : 'pc';
        if ($this->auth->id) {
//            $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('index/index'));
            $ename = $ename ? : $this->auth->class_name;
            header('location:'. url('index/Common/head',array('enname'=>$ename,'client'=>$client)));
            exit;
        }
        if ($this->request->isPost()) {
            // $username = $this->request->post('username');
            $password = $this->request->post('password');
            // $email = $this->request->post('email');
            $email = '';
            $mobile = $this->request->post('mobile', '');
            $captcha = $this->request->post('captcha');
            $token = $this->request->post('__token__');
            $rule = [
                // 'username'  => 'require|length:3,30',
                'password'  => 'require|length:6,30',
                // 'email'     => 'require|email',
                'mobile'    => 'regex:/^1\d{10}$/',
                '__token__' => 'require|token',
            ];

            $msg = [
                // 'username.require' => 'Username can not be empty',
                // 'username.length'  => 'Username must be 3 to 30 characters',
                'password.require' => '密码不能为空',
                'password.length'  => '密码长度须为6-30个字符',
                // 'email'            => 'Email is incorrect',
                'mobile'           => '手机号输入有误！',
				'password'           => 'Password is incorrect',
            ];
            $data = [
                // 'username'  => $username,
                'password'  => $password,
                // 'email'     => $email,
                'mobile'    => $mobile,
                '__token__' => $token,
            ];
            //验证码
            $captchaResult = true;
            $captchaType = config("fastadmin.user_register_captcha");
            if ($captchaType) {
                if ($captchaType == 'mobile') {
                    $captchaResult = Sms::check($mobile, $captcha, 'register');
//                } elseif ($captchaType == 'email') {
//                    $captchaResult = Ems::check($email, $captcha, 'register');
                } elseif ($captchaType == 'wechat') {
                    $captchaResult = WechatCaptcha::check($captcha, 'register');
                } elseif ($captchaType == 'text') {
                    $captchaResult = \think\Validate::is($captcha, 'captcha');
                }
            }
            if (!$captchaResult) {
                $this->error(__('Captcha is incorrect'));
            }
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            }
            $username = $mobile;
            if($this->auth->isUser($mobile)){
                if ($this->auth->login($mobile, $password)) {

                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }else{
                // Set default username, email...

                $extend['entry_platform'] = $client;
                $extend['url_source'] = $source;
                $extend['class_name'] = $ename;
                $extend['class_title'] = $cn_name;
                if ($this->auth->register($username, $password, $email, $mobile,$extend)) {

                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }

            //用户分析
            cmf_userId_set_cookieId($this->auth->id);
            cmf_cookieId_set_userId($this->auth->id);

            // ksbao 同步注册
            $website = cmf_get_website_value();
            $ksbao_data = [
                'userName' => $username,
                'password' => $password,
                'client'   => $client,
                'partnercode' => $website['partnercode'],
            ];
            $ksbao_reg_url = cmf_get_ksbao_value('nregist');
            $json = cmf_send_post($ksbao_reg_url, $ksbao_data, $website['client_ip']);
            $return_arr = json_decode($json, true);
            if ($return_arr['status'] != 200) {
                $this->error('同步失败,请联系管理员');
            } else {
                $appEName =  $ename ? :($return_arr['data']['appEName'] ?? '');
//                $appVn =  $data['vn'] ?? ($return_arr['data']['appVn'] ?? 0);
                \session('guid', $return_arr['data']['guid']);
            }
            // /ksbao 同步注册

//                $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
//            var_dump(url('Common/head',array('enname'=>$appEName,'client'=>$client)));exit;
            $this->success('注册/登录成功', url('index/Common/head',array('enname'=>$appEName,'client'=>$client)));
        }
        //判断来源
        $referer = $this->request->server('HTTP_REFERER');
        if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
            && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
            $url = $referer;
        }
        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        $this->view->assign('url', $url);
        $this->view->assign('ename', $ename);
        $this->view->assign('cn_name', $cn_name);
        $this->view->assign('source', $source);
        $this->view->assign('title', __('登陆'));
        return $this->view->fetch();
    }
	
	public function mindex(){
		$url = $this->request->request('url', '', 'trim');
        $source = $this->request->request('source', '', 'trim');
        $ename = $this->request->request('ename', '', 'trim');
        $cn_name = '';
        if($ename){
            $row = $this->model->get(['en_name'=>$ename]);
            if($row) $cn_name = $row['short_name'];
        }
        $client = cmf_is_mobile() ? 'm' : 'pc';
        if ($this->auth->id) {
            header('location:'. url('index/user/index'));
            exit;
        }
        if ($this->request->isPost()) {
            // $username = $this->request->post('username');
            $password = $this->request->post('password');
            // $email = $this->request->post('email');
            $email = '';
            $mobile = $this->request->post('mobile', '');
            $captcha = $this->request->post('captcha');
            $token = $this->request->post('__token__');
            $rule = [
                // 'username'  => 'require|length:3,30',
                'password'  => 'require|length:6,30',
                // 'email'     => 'require|email',
                'mobile'    => 'regex:/^1\d{10}$/',
                '__token__' => 'require|token',
            ];

            $msg = [
                // 'username.require' => 'Username can not be empty',
                // 'username.length'  => 'Username must be 3 to 30 characters',
                'password.require' => '密码不能为空',
                'password.length'  => '密码长度须为6-30个字符',
                // 'email'            => 'Email is incorrect',
                'mobile'           => '手机号输入有误！',
            ];
            $data = [
                // 'username'  => $username,
                'password'  => $password,
                // 'email'     => $email,
                'mobile'    => $mobile,
                '__token__' => $token,
            ];
            //验证码
            $captchaResult = true;
            $captchaType = config("fastadmin.user_register_captcha");
            if ($captchaType) {
                if ($captchaType == 'mobile') {
                    $captchaResult = Sms::check($mobile, $captcha, 'register');
//                } elseif ($captchaType == 'email') {
//                    $captchaResult = Ems::check($email, $captcha, 'register');
                } elseif ($captchaType == 'wechat') {
                    $captchaResult = WechatCaptcha::check($captcha, 'register');
                } elseif ($captchaType == 'text') {
                    $captchaResult = \think\Validate::is($captcha, 'captcha');
                }
            }
            if (!$captchaResult) {
                $this->error(__('Captcha is incorrect'));
            }
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            }
            $username = $mobile;
            if($this->auth->isUser($mobile)){
                if ($this->auth->login($mobile, $password)) {

                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }else{
                // Set default username, email...

                $extend['entry_platform'] = $client;
                $extend['url_source'] = $source;
                $extend['class_name'] = $ename;
                $extend['class_title'] = $cn_name;
                if ($this->auth->register($username, $password, $email, $mobile,$extend)) {

                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }

            //用户分析
            cmf_userId_set_cookieId($this->auth->id);
            cmf_cookieId_set_userId($this->auth->id);

			// ksbao 同步注册
            $website = cmf_get_website_value();
            $ksbao_data = [
                'userName' => $username,
                'password' => $password,
                'client'   => $client,
                'partnercode' => $website['partnercode'],
            ];
            $ksbao_reg_url = cmf_get_ksbao_value('nregist');
            $json = cmf_send_post($ksbao_reg_url, $ksbao_data, $website['client_ip']);
            $return_arr = json_decode($json, true);
            if ($return_arr['status'] != 200) {
                $this->error('同步失败,请联系管理员');
            } else {
                $appEName =  $ename ? :($return_arr['data']['appEName'] ?? '');
//                $appVn =  $data['vn'] ?? ($return_arr['data']['appVn'] ?? 0);
                \session('guid', $return_arr['data']['guid']);
            }
			$this->success('注册/登录成功', url('index/user/index'));

        }
        //判断来源
        $referer = $this->request->server('HTTP_REFERER');
        if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
            && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
            $url = $referer;
        }
        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        $this->view->assign('url', $url);
        $this->view->assign('ename', $ename);
        $this->view->assign('cn_name', $cn_name);
        $this->view->assign('source', $source);
        $this->view->assign('title', __('登陆'));
        return $this->view->fetch();
	}
	
	//找回密码
	public function findpassword(){
		if($this->request->isPost()){
            $password = $this->request->post('password');
            $repassword = $this->request->post('repassword');
            $mobile = $this->request->post('mobile', '');
            $captcha = $this->request->post('captcha');
            $token = $this->request->post('__token__');
            $rule = [
                // 'username'  => 'require|length:3,30',
                'password'  => 'require|length:6,30',
                'repassword'  => 'require|length:6,30',
                // 'email'     => 'require|email',
                'mobile'    => 'regex:/^1\d{10}$/',
                '__token__' => 'require|token',
            ];

            $msg = [
                // 'username.require' => 'Username can not be empty',
                // 'username.length'  => 'Username must be 3 to 30 characters',
                'password.require' => 'Password can not be empty',
                'repassword.require' => 'RePassword can not be empty',
                'password.length'  => 'Password must be 6 to 30 characters',
                'repassword.length'  => 'RerePassword must be 6 to 30 characters',
                // 'email'            => 'Email is incorrect',
                'mobile'           => 'Mobile is incorrect',
            ];
            $data = [
                // 'username'  => $username,
                'password'  => $password,
                'repassword'  => $repassword,
                // 'email'     => $email,
                'mobile'    => $mobile,
                '__token__' => $token,
            ];
			if($password != $repassword) $this->error('两次输入的密码不一样');
			//验证码
            $captchaResult = true;
            //$captchaType = config("fastadmin.user_register_captcha");
            $captchaType = 'mobile';
            if ($captchaType) {
                if ($captchaType == 'mobile') {
                    //$captchaResult = Sms::check($mobile, $captcha, 'register');
                    $captchaResult = Sms::check($mobile, $captcha, 'captcha');
//                } elseif ($captchaType == 'email') {
//                    $captchaResult = Ems::check($email, $captcha, 'register');
                } elseif ($captchaType == 'wechat') {
                    $captchaResult = WechatCaptcha::check($captcha, 'register');
                } elseif ($captchaType == 'text') {
                    $captchaResult = \think\Validate::is($captcha, 'captcha');
                }
            }
            if (!$captchaResult) {
                $this->error(__('Captcha is incorrect'));
            }
			$validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            }
			$user = \app\common\model\User::getByMobile($mobile);
            if (!$user) {
                $this->error(__('未注册'));
            }
			$this->auth->direct($user->id);
			$ret = $this->auth->changepwd($password, '', true);
			if ($ret) {
				$this->auth->logout();
				$this->success(__('密码重置成功'),url('user/login/'));
			} else {
				$this->error($this->auth->getError());
			}
		}else{
			$this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
			$this->view->assign('cn_name', '');
			$this->view->assign('title', __('找回密码'));
			return $this->view->fetch();
		}
	}
	
	
	//验证码登录
	/**
     * 注册会员
     */
    public function indexnew()
    {
        $url = $this->request->request('url', '', 'trim');
        $source = $this->request->request('source', '', 'trim');
        $ename = $this->request->request('ename', '', 'trim');
        $cn_name = '';
        if($ename){
            $row = $this->model->get(['en_name'=>$ename]);
            if($row) $cn_name = $row['short_name'];
        }
        $client = cmf_is_mobile() ? 'm' : 'pc';
        if ($this->auth->id) {
//            $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('index/index'));
            $ename = $ename ? : $this->auth->class_name;
            header('location:'. url('index/Common/head',array('enname'=>$ename,'client'=>$client)));
            exit;
        }
        if ($this->request->isPost()) {
            // $username = $this->request->post('username');
            $password = $this->request->post('password');
            // $email = $this->request->post('email');
            $email = '';
            $mobile = $this->request->post('mobile', '');
            $captcha = $this->request->post('captcha');
            $type = $this->request->post('captcha');
			$type = $type == 'password' ? $type : 'captcha';
            $token = $this->request->post('__token__');
            $rule = [
                // 'username'  => 'require|length:3,30',
                //'password'  => 'require|length:6,30',
                // 'email'     => 'require|email',
                'mobile'    => 'regex:/^1\d{10}$/',
                '__token__' => 'require|token',
            ];
			if($type == 'password'){
				$rule['password'] = 'require|length:6,30';
				$msg['password.require'] = '密码不能为空';
				$msg['password.length'] = '密码长度须为6-30个字符';
				$data['password'] = $password;
			}

            $msg = [
                // 'username.require' => 'Username can not be empty',
                // 'username.length'  => 'Username must be 3 to 30 characters',
                //'password.require' => '密码不能为空',
                //'password.length'  => '密码长度须为6-30个字符',
                // 'email'            => 'Email is incorrect',
                'mobile'           => '手机号输入有误！',
            ];
            $data = [
                // 'username'  => $username,
                //'password'  => $password,
                // 'email'     => $email,
                'mobile'    => $mobile,
                '__token__' => $token,
            ];
            //验证码
            $captchaResult = true;
            //$captchaType = config("fastadmin.user_register_captcha");
            $captchaType = $type == 'captcha' ? 'mobile' : false;
            /* if ($captchaType) {
                if ($captchaType == 'mobile') {
                    //$captchaResult = Sms::check($mobile, $captcha, 'register');
                    $captchaResult = Sms::check($mobile, $captcha, 'captcha');
//                } elseif ($captchaType == 'email') {
//                    $captchaResult = Ems::check($email, $captcha, 'register');
                } elseif ($captchaType == 'wechat') {
                    $captchaResult = WechatCaptcha::check($captcha, 'register');
                } elseif ($captchaType == 'text') {
                    $captchaResult = \think\Validate::is($captcha, 'captcha');
                }
            } */
            if (!$captchaResult) {
                $this->error(__('Captcha is incorrect'));
            }
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            }
            $username = $mobile;
            if($this->auth->isUser($mobile)){
				if($type == 'password'){
					if ($this->auth->login($mobile, $password)) {

					} else {
						$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
					}
				}else{
					if ($this->auth->loginCaptcha($mobile, $password)) {

					} else {
						$this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
					}
				}
                
            }else{
                // Set default username, email...

                $extend['entry_platform'] = $client;
                $extend['url_source'] = $source;
                $extend['class_name'] = $ename;
                $extend['class_title'] = $cn_name;
				if($type == 'captcha') $password = '123456';
				
                if ($this->auth->register($username, $password, $email, $mobile,$extend)) {
					if($type == 'captcha'){
						//发送短信 密码
						$config = get_addon_config('alisms');
						//var_dump($config);exit;
						$sign = $config['sign'];
						$template = $config['template']['sendPassword'];
						$param = ['password'=>$password];
						$alisms = new \addons\alisms\library\Alisms();
						$ret = $alisms->mobile($username)
							->template($template)
							->sign($sign)
							->param($param)
							->send();
						/* if ($ret) {
							// $this->success("发送成功,记得保存哦,否则CDK将不会有记录");
							$this->success("发送成功");
						} else {
							$this->error("发送失败！失败原因：" . $alisms->getError());
						} */
					} 
					
                } else {
                    $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
                }
            }

            //用户分析
            cmf_userId_set_cookieId($this->auth->id);
            cmf_cookieId_set_userId($this->auth->id);

            // ksbao 同步注册
            $website = cmf_get_website_value();
            $ksbao_data = [
                'userName' => $username,
                //'password' => $password,
                'client'   => $client,
                'partnercode' => $website['partnercode'],
            ];
			if($type == 'password') $ksbao_data['password'] = $password;
            $ksbao_reg_url = cmf_get_ksbao_value('nregist');
            $json = cmf_send_post($ksbao_reg_url, $ksbao_data, $website['client_ip']);
            $return_arr = json_decode($json, true);
			var_dump($return_arr);exit;
            if ($return_arr['status'] != 200) {
                $this->error('同步失败,请联系管理员');
            } else {
                $appEName =  $ename ? :($return_arr['data']['appEName'] ?? '');
//                $appVn =  $data['vn'] ?? ($return_arr['data']['appVn'] ?? 0);
                \session('guid', $return_arr['data']['guid']);
            }
            // /ksbao 同步注册

//                $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
//            var_dump(url('Common/head',array('enname'=>$appEName,'client'=>$client)));exit;
            $this->success('注册/登录成功', url('index/Common/head',array('enname'=>$appEName,'client'=>$client)));
        }
        //判断来源
        $referer = $this->request->server('HTTP_REFERER');
        if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
            && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
            $url = $referer;
        }
        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        $this->view->assign('url', $url);
        $this->view->assign('ename', $ename);
        $this->view->assign('cn_name', $cn_name);
        $this->view->assign('source', $source);
        $this->view->assign('title', __('登陆'));
        return $this->view->fetch();
    }

}
